package com.hq.bus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.bus.domain.Customer;
import com.hq.bus.domain.Rent;
import com.hq.bus.mapper.RentMapper;
import com.hq.sys.util.DataGridView;
import com.hq.bus.mapper.CustomerMapper;
import com.hq.bus.service.CustomerService;
import com.hq.bus.vo.CustomerVo;
import com.hq.sys.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {


    @Autowired
    private CustomerMapper customerMapper;


    /**
     * 查询所有客户信息 分⻚
     *
     * @param customerVo
     * @return
     */
    @Override
    public DataGridView queryAllCustomer(CustomerVo customerVo) {
        Page<Object> page = PageHelper.startPage(customerVo.getPage(), customerVo.getLimit());
       // List<Customer> data = this.customerMapper.selectAll();
        Weekend<Customer> weekend = Weekend.of(Customer.class);
        if (StrUtil.isNotBlank(customerVo.getIdentity())){
            weekend.weekendCriteria().andEqualTo(Customer::getIdentity, customerVo.getIdentity());
        }
        if (StrUtil.isNotBlank(customerVo.getCustname())){
            weekend.weekendCriteria().andLike(Customer::getCustname, "%" + customerVo.getCustname() + "%");
        }
        if(StrUtil.isNotBlank(customerVo.getAddress())){
            weekend.weekendCriteria().andLike(Customer::getAddress, "%" + customerVo.getAddress() + "%");
        }
        if (StrUtil.isNotBlank(customerVo.getPhone())){
            weekend.weekendCriteria().andLike(Customer::getPhone, "%" + customerVo.getPhone() + "%");
        }
        if (Objects.nonNull(customerVo.getSex())){
            weekend.weekendCriteria().andEqualTo(Customer::getSex, customerVo.getSex());
        }
        if (StrUtil.isNotBlank(customerVo.getCareer())){
            weekend.weekendCriteria().andLike(Customer::getCareer, "%" + customerVo.getCareer() + "%");
        }
        List<Customer> data = customerMapper.selectByExample(weekend);
        return new DataGridView(page.getTotal(), data);
    }

    @Override
    public Customer queryCustomerByIdentity(String identity) {
        return customerMapper.selectByPrimaryKey(identity);
    }

    //添加客户
    @Override
    public int addCustomer(CustomerVo customerVo) {
        return customerMapper.insertSelective(customerVo);
    }
    //删除客户
    @Override
    public void deleteCustomer(String identity) {
         customerMapper.deleteByPrimaryKey(identity);
    }
    //批量删除客户
    @Override
    public void deleteBatchCustomer(String[] identitys) {
        Arrays.asList(identitys).stream().forEach(identity->customerMapper.deleteByPrimaryKey(identity));

    }
    //修改客户
    @Override
    public int updateCustomer(CustomerVo customerVo) {
        return customerMapper.updateByPrimaryKeySelective(customerVo);
    }
}